//
//  YCCommentsViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/23.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCCommentsViewController.h"
#define YCUnReplyCellIdentify @"YCUnReplyTableViewCell"
#define YCGoCommentCellIdentify @"YCGoCommentTableViewCell"
#define YCRepliedCellIdentify @"YCRepliedTableViewCell"
@interface YCCommentsViewController ()
{
    UIButton *m_btnContent;
    
    UIButton *m_btnUnreply;
    UIButton *m_btnAll;
    UILabel *m_labLine;
    
    UIScrollView* _contentscrollview;
    UITableView *_tableView1;
    UITableView *_tableView2;
    
    int _currentPage;
    NSMutableArray *_arrayList1;
    NSMutableArray *_arrayList2;
}
@end

@implementation YCCommentsViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"评价";
    self.navigationItem.leftBarButtonItem=[self tbarBackButtonWhite];
    
    
    _currentPage=1;
    _arrayList1=[NSMutableArray array];
    _arrayList2=[NSMutableArray array];
    
    [self addMainView];
    [self loadData];
}
-(void)loadData
{
    OrderCommentIndexRequest *request = [OrderCommentIndexRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    if (m_btnUnreply.selected) {
        request.status=@"1";
        request.have_content=[NSString stringWithFormat:@"%d",m_btnContent.selected];
    }else{
        request.have_content=[NSString stringWithFormat:@"%d",m_btnContent.selected];
    }
    request.page=[NSString stringWithFormat:@"%ld",(long)_currentPage];
    [apiClient doOrderCommentIndex:request success:^(ApiResponse *data, NSString *url) {
        OrderCommentIndexResponse *response=[[OrderCommentIndexResponse alloc]initWithCGResponse:data];

        [m_btnUnreply setTitle:[NSString stringWithFormat:@"未回复评价 (%@)",response.no_reply] forState:UIControlStateNormal];

        if (m_btnUnreply.selected) {
            if (_currentPage>1) {
                [_arrayList1 addObjectsFromArray:response.lists];
            }else{
                _arrayList1=[NSMutableArray arrayWithArray:response.lists];
            }
            if (response.lists.count==0) {
                _tableView1.mj_footer.state=MJRefreshStateNoMoreData;
            }else{
                [_tableView1.mj_footer endRefreshing];
            }
            
            [_tableView1 reloadData];
        }else{
            if (_currentPage>1) {
                [_arrayList2 addObjectsFromArray:response.lists];
            }else{
                _arrayList2=[NSMutableArray arrayWithArray:response.lists];
            }
            if (response.lists.count==0) {
                _tableView2.mj_footer.state=MJRefreshStateNoMoreData;
            }else{
                [_tableView2.mj_footer endRefreshing];
            }
            [_tableView2 reloadData];
        }
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addMainView
{
    UIView *backview=[[UIView alloc]initWithFrame:CGRectMake(0, kNAV_HEIGHT, kMainScreen_Width, 36)];
    [backview setBackgroundColor:MAIN_COLOR];
    [self.view addSubview:backview];
    
    CGFloat width=kMainScreen_Width/2;
    m_labLine=[[UILabel alloc]initWithFrame:CGRectMake(width/2-S(20), H(backview)-3, S(40), 3)];
    [m_labLine setBackgroundColor:WHITE_COLOR];
    [backview addSubview:m_labLine];

    
    m_btnUnreply=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, H(backview))];
    [m_btnUnreply setTitleColor:getUIColor(0xffc3c7) forState:UIControlStateNormal];
    [m_btnUnreply setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
    [m_btnUnreply setTitle:@"未回复评价" forState:UIControlStateNormal];
    [m_btnUnreply.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [m_btnUnreply setSelected:YES];
    [backview addSubview:m_btnUnreply];
    [m_btnUnreply addTarget:self action:@selector(buttonOrderClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    m_btnAll=[[UIButton alloc]initWithFrame:CGRectMake(width, 0, width, H(backview))];
    [m_btnAll.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [m_btnAll setTitle:@"全部评价" forState:UIControlStateNormal];
    [m_btnAll setSelected:NO];
    [m_btnAll setTitleColor:getUIColor(0xffc3c7) forState:UIControlStateNormal];
    [m_btnAll setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
    [backview addSubview:m_btnAll];
    [m_btnAll addTarget:self action:@selector(buttonOrderClicked:) forControlEvents:UIControlEventTouchUpInside];
    

    UIView *headerview=[[UIView alloc]init];
    [headerview setBackgroundColor:WHITE_COLOR];
    [self.view addSubview:headerview];
    [headerview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(backview.bottom).offset(0);
        make.height.mas_equalTo(40);
    }];
    
    m_btnContent=[[UIButton alloc]initWithFrame:CGRectMake(kDistance, 0, 200, 40)];
    [m_btnContent setImage:[UIImage imageNamed:@"comment_unselect"] forState:UIControlStateNormal];
    [m_btnContent setImage:[UIImage imageNamed:@"comment_select"] forState:UIControlStateSelected];
    [m_btnContent setTitle:@"只看有内容的评价" forState:UIControlStateNormal];
    [m_btnContent setTitleColor:TEXT_LIGHT forState:UIControlStateNormal];
    [m_btnContent.titleLabel setFont:[UIFont LightFontOfSize:14]];
    [m_btnContent addTarget:self action:@selector(btnContentClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_btnContent.titleLabel setFont:[UIFont LightFontOfSize:13]];
    [m_btnContent setImageEdgeInsets:UIEdgeInsetsMake(13, 0, 13, 187)];
    [m_btnContent setTitleEdgeInsets:UIEdgeInsetsMake(13, -24, 13, 60)];
    m_btnContent.selected=YES;
    [headerview addSubview:m_btnContent];
    
    
    CGFloat height=kMainScreen_Height-H(backview)-kNAV_HEIGHT-40;
    _contentscrollview = [[UIScrollView alloc]init];
    [self.view addSubview:_contentscrollview];
    _contentscrollview.delegate = self;
    [_contentscrollview setContentSize:CGSizeMake(kMainScreen_Width*2, height)];
    _contentscrollview.pagingEnabled = YES;
    _contentscrollview.bounces = NO;
    _contentscrollview.showsHorizontalScrollIndicator=NO;
    [_contentscrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(headerview.bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];

    _tableView1 =[self addTableView:0];
    _tableView2 =[self addTableView:1];
}
-(UITableView*)addTableView:(NSInteger)page{
    CGFloat height=kMainScreen_Height-36-kNAV_HEIGHT-40;
    UITableView *tableview=[[UITableView alloc]initWithFrame:CGRectMake(page*kMainScreen_Width,0, kMainScreen_Width,height)];
    [tableview setBackgroundColor:BACK_COLOR];
    tableview.delegate=self;
    tableview.dataSource=self;
    tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [tableview registerNib:[UINib nibWithNibName:YCUnReplyCellIdentify bundle:nil] forCellReuseIdentifier:YCUnReplyCellIdentify];
    [tableview registerNib:[UINib nibWithNibName:YCGoCommentCellIdentify bundle:nil] forCellReuseIdentifier:YCGoCommentCellIdentify];
    [tableview registerNib:[UINib nibWithNibName:YCRepliedCellIdentify bundle:nil] forCellReuseIdentifier:YCRepliedCellIdentify];
    
    tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;
        [self loadData];
        [tableview.mj_header endRefreshing];
        
        tableview.mj_footer.state=MJRefreshStateIdle;
    }];
    tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [self loadData];
    }];
    
    [_contentscrollview addSubview:tableview];
    return tableview;
}

-(void)buttonOrderClicked:(UIButton *)button
{
    if (button==m_btnUnreply) {
        [m_btnUnreply setSelected:YES];
        [m_btnAll setSelected:NO];
        [self scrollLineWith:YES];
        [_contentscrollview  setContentOffset:CGPointMake(0, 0) animated:NO];
    }else{
        [m_btnUnreply setSelected:NO];
        [m_btnAll setSelected:YES];
        [self scrollLineWith:NO];
        [_contentscrollview  setContentOffset:CGPointMake(W(_contentscrollview), 0) animated:NO];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_contentscrollview) {
        NSInteger index = fabs(_contentscrollview.contentOffset.x) / _contentscrollview.frame.size.width;
        if(index==0){
            _currentPage=1;
            [m_btnUnreply setSelected:YES];
            [m_btnAll setSelected:NO];
            [self scrollLineWith:YES];
        }else {
            _currentPage=1;
            [m_btnUnreply setSelected:NO];
            [m_btnAll setSelected:YES];
            [self scrollLineWith:NO];
        }
    }
}
-(void)scrollLineWith:(BOOL)left
{
    if (!left) {
        [m_btnUnreply.titleLabel setFont:[UIFont LightFontOfSize:15]];
        [m_btnAll.titleLabel setFont:[UIFont LightFontOfSize:16]];
        [UIView animateWithDuration:0.3 animations:^{
            m_labLine.transform = CGAffineTransformMakeTranslation(kMainScreen_Width*1/2, 0);
        }];
    }else{
        [m_btnUnreply.titleLabel setFont:[UIFont LightFontOfSize:16]];
        [m_btnAll.titleLabel setFont:[UIFont LightFontOfSize:15]];
        [UIView animateWithDuration:0.3 animations:^{
            m_labLine.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
    }
    [self loadData];
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==_tableView1) {
        return _arrayList1.count;
    }
    return _arrayList2.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_tableView1) {
        CommentTable *comment=_arrayList1[indexPath.section];
        
        CGFloat height=getTextSize(comment.user_content, kMainScreen_Width-60, [UIFont LightFontOfSize:15]).height-20;
        
        CGFloat w = 0;
        CGFloat h = 15;
        NSArray *remarks=[comment.tags_content componentsSeparatedByString:@","];
        for (int i=0; i<remarks.count; i++) {
            CGFloat width=getTextSize(remarks[i], kMainScreen_Width, [UIFont LightFontOfSize:12]).width+20;
            if (w+width>kMainScreen_Width-60) {
                w=0;
                h=h+28;
            }
            w = w+width+10;
        }
        if (remarks.count>0) {
            height=height+h+20;
        }
        
        if (comment.selected) {
            if (comment.images.count>0) {
                return 335+height;
            }
            return 265+height;
        }else{
            if (comment.tags_content.length==0) {
                height=height-40;
            }
            if (comment.status.intValue==2) {
                height=height+getTextSize(comment.shop_content, kMainScreen_Width-60, [UIFont LightFontOfSize:15]).height-20;
                if (comment.tags_content.length==0) {
                    height=height-40;
                }
                if (comment.images.count>0) {
                    return 205+height;
                }
                return 145;
            }else{
                if (comment.images.count>0) {
                    return 175+height;
                }
                return 115+height;
            }
        }
    }
    if (tableView==_tableView2) {
        CommentTable *comment=_arrayList2[indexPath.section];
         CGFloat height=getTextSize(comment.user_content, kMainScreen_Width-60, [UIFont LightFontOfSize:15]).height-20;
        
        CGFloat w = 0;
        CGFloat h = 15;
        NSArray *remarks=[comment.tags_content componentsSeparatedByString:@","];
        for (int i=0; i<remarks.count; i++) {
            CGFloat width=getTextSize(remarks[i], kMainScreen_Width, [UIFont LightFontOfSize:12]).width+20;
            if (w+width>kMainScreen_Width-60) {
                w=0;
                h=h+28;
            }
            w = w+width+10;
        }
        if (remarks.count>0) {
            height=height+h+20;
        }
        
        if (comment.selected) {
            if (comment.images.count>0) {
                return 335+height;
            }
            return 265+height;
        }else{
            if (comment.tags_content.length==0) {
                height=height-40;
            }
            if (comment.status.intValue==2) {
                height=height+getTextSize(comment.shop_content, kMainScreen_Width-60, [UIFont LightFontOfSize:15]).height-20;
                if (comment.images.count>0) {
                    return 205+height;
                }else{
                    return 145+height;
                }
            }else{
                if (comment.images.count>0) {
                    return 175+height;
                }
                return 115+height;
            }
        }
    }
    return CGFLOAT_MIN;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (tableView==_tableView1) {
        CommentTable *comment=_arrayList1[indexPath.section];
        if (comment.selected) {
            YCGoCommentTableViewCell *_cell = (YCGoCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoCommentCellIdentify forIndexPath:indexPath];
            _cell.cancelBlock=^(){
                comment.selected=!comment.selected;
                [_tableView1 reloadData];
            };
            _cell.replyBlock=^(NSString *reply){
                comment.selected=!comment.selected;
                if (reply.length>0) {
                    [self loadCommentWith:comment Text:reply];
                }
                [_tableView1 reloadData];
            };
            [_cell bindWith:comment];
            cell=_cell;
        }else{
            if (comment.status.intValue==2) {
                YCRepliedTableViewCell *_cell = (YCRepliedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCRepliedCellIdentify forIndexPath:indexPath];
                [_cell bindWith:comment];
                cell=_cell;
            }else{
                YCUnReplyTableViewCell *_cell = (YCUnReplyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCUnReplyCellIdentify forIndexPath:indexPath];
                _cell.replyBlock=^(){
                    comment.selected=!comment.selected;
                    [_tableView1 reloadData];
                };
                [_cell bindWith:comment];
                cell=_cell;
            }
        }
    }else{
        CommentTable *comment=_arrayList2[indexPath.section];
        if (comment.selected) {
            YCGoCommentTableViewCell *_cell = (YCGoCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoCommentCellIdentify forIndexPath:indexPath];
            _cell.cancelBlock=^(){
                comment.selected=!comment.selected;
                [_tableView2 reloadData];
            };
            _cell.replyBlock=^(NSString *reply){
                comment.selected=!comment.selected;
                if (reply.length>0) {
                    [self loadCommentWith:comment Text:reply];
                }
                [_tableView2 reloadData];
            };
            [_cell bindWith:comment];
            cell=_cell;
        }else{
            if (comment.status.intValue==2) {
                YCRepliedTableViewCell *_cell = (YCRepliedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCRepliedCellIdentify forIndexPath:indexPath];
                [_cell bindWith:comment];
                cell=_cell;
            }else{
                YCUnReplyTableViewCell *_cell = (YCUnReplyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCUnReplyCellIdentify forIndexPath:indexPath];
                _cell.replyBlock=^(){
                     comment.selected=!comment.selected;
                    [_tableView2 reloadData];
                };
                [_cell bindWith:comment];
                cell=_cell;
            }
        }
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)btnContentClicked:(UIButton *)button
{
    if (button.selected) {
        [button setSelected:NO];
    }else{
        [button setSelected:YES];
    }
    _currentPage=1;
    [self loadData];
}
-(void)loadCommentWith:(CommentTable *)comment Text:(NSString *)text
{
    OrderCommentUpdateRequest *request = [OrderCommentUpdateRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    request.Id=comment.Id;
    request.content=text;
    [apiClient doOrderCommentUpdate:request success:^(ApiResponse *data, NSString *url) {
        [self loadData];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
@end







